//
//  SEAuthApi.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/18.
//

import Moya

enum SEAuthAPI {
    case register(phone: String, code: String, password: String, source: Int = 2)
    case login(account: String, password: String, sysCode: String = "XTBM-YKAPP", roleCode: String = "SHJS-HZ", clientType: String = "KHDLX-APP", loginModel: Int = 1)
    case getMsg(phone: String)
    case findPassword(account: String, password: String, phone: String, code: String, sysCode: String = "XTBM-YKAPP", roleCode: String = "SHJS-HZ", clientType: String = "KHDLX-APP")
    case changePassword(account: String, oldPassword: String, password: String, sysCode: String = "XTBM-YKAPP", roleCode: String = "SHJS-HZ", clientType: String = "KHDLX-APP")
    case bindPushAccount(devNum: String)
    case logout
}

extension SEAuthAPI: TargetType {
    var baseURL: URL {
        return URL(string: SEApiManager.baseUrl)!
    }

    var path: String {
        switch self {
        case .register: return "/smarteye/smarteye-base/user/register"
        case .login: return "/ng/newfuture-auth/user/login"
        case .getMsg: return "/ng/newfuture-msg/sms/vCode"
        case .findPassword: return "/ng/newfuture-auth/user/findPasswd"
        case .changePassword: return "/ng/newfuture-auth/user/modifyPasswd"
        case .bindPushAccount: return "/ng/newfuture-auth/pushAccount/bindPushAccount"
        case .logout: return "/ng/newfuture-auth/user/logout"
        }
    }

    var method: Moya.Method {
        return .post
    }

    var sampleData: Data {
        return "".data(using: .utf8)!
    }

    var task: Task {
        return .requestParameters(parameters: parameters, encoding: JSONEncoding.default)
    }

    var headers: [String : String]? {
        return SEApiManager.headers
    }

    private var parameters: [String: Any] {
        switch self {
        case let .register(phone, code, password, source):
            return ["phone": phone,
                    "code": code,
                    "password": password,
                    "source": source]
        case let .login(account, password, sysCode, roleCode, clientType, loginModel):
            return ["account": account,
                    "passwd": password,
                    "sysCode": sysCode,
                    "roleCode": roleCode,
                    "clientType": clientType,
                    "loginModel": loginModel]
        case let .getMsg(phone):
            return ["phone": phone]
        case let .findPassword(account, password, phone, code, sysCode, roleCode, clientType):
            return ["account": account,
                    "newPasswd": password,
                    "mobilephone": phone,
                    "sysCode": sysCode,
                    "phoneVerifyCode": code,
                    "roleCode": roleCode,
                    "clientType": clientType]
        case let .changePassword(account, oldPassword, password, sysCode, roleCode, clientType):
            return ["account": account,
                    "oldPasswd": oldPassword,
                    "newPasswd": password,
                    "sysCode": sysCode,
                    "roleCode": roleCode,
                    "clientType": clientType]
        case let .bindPushAccount(devNum):
            return ["devNum": devNum,
                    "osType": "1",
                    "sysCode": "XTBM-YKAPP",
                    "clientType": "KHDLX-APP",
                    "puid": UserDefaults.userId]
        case .logout:
            return [:]
        }
    }
}
